(function (angular) {
    'use strict';

    var module = angular.module('sinbad.controllers.semiautos',
        ['ngResource', 'highcharts-ng', 'ui.bootstrap.modal']);


    module.controller('SemiautosCtrl', ['$scope', '$uibModal', function ($scope, $modal) {
        $scope.semiautos = [
            {
                id: 323,
                name: "思路1",
                object: "推广计划",
                days: 1,
                time: "2016-03-26",
                type: "精确引流",
                platform: 1
            },
            {
                id: 324,
                name: "思路2",
                object: "推广计划",
                days: 30,
                time: "2016-03-26",
                type: "精确引流",
                platform: 1
            }
        ];

        $scope.semiauto = {};

        $scope.openSemiautosEditor = function () {
            var modalInstance = $modal.open({
                templateUrl: 'views/semiautos/my-semiautos_editor.html',
                controller: 'SemiautosEditorCtrl',
                size: 300,
                resolve: {
                    semiauto: function () {
                        return $scope.semiauto;
                    }
                }
            });

            modalInstance.result.then(function (semiauto) {
                console.log('semiauto', semiauto);
                $scope.semiautos.push(semiauto);
                $scope.semiauto = {};
            }, function () {

            });
        };

        $scope.editSemiauto = function () {
            var modalInstance = $modal.open({
                templateUrl: 'views/semiautos/my-semiautos_editor.html',
                controller: 'SemiautosEditorCtrl',
                size: 300,
                resolve: {
                    semiauto: function () {
                        return $scope.semiauto;
                    }
                }
            });

            modalInstance.result.then(function (semiauto) {
                console.log('semiauto', semiauto);
                $scope.semiautos.push(semiauto);
                $scope.semiauto = {};
            }, function () {

            });
        };
    }]);

    module.controller('SemiautosEditorCtrl', ['$scope', '$uibModalInstance', 'semiauto',
        function ($scope, $uibModalInstance, semiauto) {
            $scope.semiauto = semiauto;
            $scope.semiauto.id = '000';
            $scope.semiauto.name = "思路";
            $scope.semiauto.platform = $scope.semiauto.platform || '1';
            $scope.semiauto.days = $scope.semiauto.days || '1';
            $scope.semiauto.time = new Date();
            $scope.semiauto.type = $scope.semiauto.type || '1';
            $scope.semiauto.symbol = $scope.semiauto.symbol || '1';

            $scope.semiauto.conditions = [
                {
                    name: '1',
                    condition: '=',
                    num:"",
                    value: "1" ,
                    source_value: '',
                    method: 'and'
                }
            ];

            $scope.semiauto.match_model = {
                status: '1',
                value: '0'
            };

            $scope.semiauto.price_model = {
                status:  '1',
                price_type: '1',
                option_type:  '1',
                price_value:  '0',
                extreme:10
            };

            $scope.semiauto.pricing = ( $scope.semiauto.price_model.status || $scope.semiauto.match_model.status) ? true : false;

            $scope.addCondition = function (method) {
                var condition = {
                    name: '1',
                    condition: '=',
                    num:"",
                    value: '1',
                    source_value: '',
                    method: method
                };
                console.log('condition', condition);
                $scope.semiauto.conditions.push(condition);
            };

            $scope.ok = function () {
                if ($scope.conditions > 0) {
                    name = $filter(name)(name);
                }
                if ($scope.semiauto.pricing) {
                    $scope.semiauto.price_model.status = '1';
                    $scope.semiauto.match_model.status = '1';
                }
                $uibModalInstance.close($scope.semiauto);
            };
            $scope.cancel = function () {
                $uibModalInstance.dismiss('cancel');
            };
        }]);

    module.controller('PlatformSemiautosCtrl', ['$scope', function ($scope) {
        $scope.commonSemiautos = [
            {id: 1, name: '培养包', title: 'http://placehold.it/200x200'},
            {id: 2, name: '培养包', title: 'http://placehold.it/200x200'}
        ];
        $scope.categorySemiautos = [
            {id: 3, name: '培养包', title: 'http://placehold.it/200x200'},
            {id: 4, name: '培养包', title: 'http://placehold.it/200x200'},
            {id: 5, name: '培养包', title: 'http://placehold.it/200x200'},
            {id: 6, name: '培养包', title: 'http://placehold.it/200x200'}
        ];
        $scope.skillSemiautos = [
            {id: 7, name: '培养包', title: 'http://placehold.it/200x200'},
            {id: 8, name: '培养包', title: 'http://placehold.it/200x200'},
            {id: 9, name: '培养包', title: 'http://placehold.it/200x200'},
            {id: 10, name: '培养包', title: 'http://placehold.it/200x200'}
        ];
    }]);

})(angular);